package com.zhengqiangblog.blog.web;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.zhengqiangblog.blog.exception.ResourceNotFoundException;
import com.zhengqiangblog.blog.model.Article;
import com.zhengqiangblog.blog.service.ArticleService;
import com.zhengqiangblog.blog.service.SideBarService;
import com.zhengqiangblog.core.ApplicationContext;
import com.zhengqiangblog.core.Model;

public class ArticleAction {

	private static final Logger logger = Logger.getLogger(ArticleAction.class);

	private SideBarService sideBarService = ApplicationContext.get(SideBarService.class);
	private ArticleService articleService = ApplicationContext.get(ArticleService.class);

	public ArticleAction() {
		logger.info("ArticleAction initializing...");
	}

	public String execute(Model model) throws Exception {
		String strId = model.getPathVariable("id");
		if (StringUtils.isNoneBlank(strId)) {
			int id = Integer.parseInt(strId);
			Article article = articleService.getArticleAndIncreseViews(id);
			if (article != null) {
				model.addAttribute("article", article);
			} else {
				throw new ResourceNotFoundException();
			}
		}
		sideBarService.initialize(model);
		return "ARTICLE";
	}

}
